cd ${work}5_reg_analysis

local dumlist	"dum_income"


*** FDY LEVEL
local ylist		"exp_status log_vrd_exp_exsmall"
local felist	`" "IDY FD" "IDY FD FY" "'
local wlist		`" "c.sales##c.sales##c.sales##c.sales" "" "'

use temp\regsample_idt, clear

eststo clear
local mtitles
local n=1
foreach y of local ylist {

local nfe=1	
foreach fe of local felist {
local w : word `nfe' of `wlist'

local estlist_ols
local estlist_iv

local m=1
foreach x of local dumlist {
	
foreach l of numlist 1 0 {
	cap gen x`l'_ols_`m' = treated_2 * (`x'==`l')	
	cap gen x`l'_iv_`m' = treated_2 * (`x'==`l')
	gen z`l' = tdcinitiative_2 * (`x'==`l')
}	
foreach l in ols iv {
	lab var x1_`l'_`m' "High $ \times \overline{support}_{fdt} $"
	lab var x0_`l'_`m' "Low $ \times \overline{support}_{fdt} $"
}	

	eststo est_ols_`m' : reghdfe `y' x1_ols_`m' x0_ols_`m' `w', a(`fe') vce(cl F) nosamp	
	local N = e(N)
	local estlist_ols `estlist_ols' est_ols_`m'
	
	eststo est_iv_`m' : ivreghdfe `y' ( x1_iv_`m' x0_iv_`m' = z1 z0 ) `w', a(`fe') cl(F) ffirst
	drop z1 z0
	local estlist_iv `estlist_iv' est_iv_`m'
	
	matrix first = e(first)
	local F1_`m' = first[8,1]
	local F0_`m' = first[8,2]
	matrix drop first
	
	local ++m
	
}

	eststo stacked_`n' : appendmodels `estlist_ols' `estlist_iv'
	qui estadd scalar N `N'
local m=1	
foreach x of local dumlist {
	qui estadd scalar F1_`m' `F1_`m''
	qui estadd scalar F0_`m' `F0_`m''
	local ++m
}
	qui estadd local FE `fe'
	if "`w'"!=""	qui estadd local SC "Yes"
	if "`w'"==""	qui estadd local SC "No"
	qui estadd local LO "FDY"		
	local mtitles `mtitles' "`:var lab `y''"
		
	eststo drop est*
	local ++n
	local ++nfe
	
}	
}
	

*** FPDY LEVEL
local ylist		"log_quantity log_price"
local felist	`" "IDY FD PY" "IDY FPD FPY" "'
local wlist		`" "c.sales##c.sales##c.sales##c.sales" "" "'

use temp\regsample_ipdt, clear

foreach y of local ylist {

local nfe=1	
foreach fe of local felist {
local w : word `nfe' of `wlist'

local estlist_ols
local estlist_iv

local m=1
foreach x of local dumlist {
	
foreach l of numlist 1 0 {
	cap gen x`l'_ols_`m' = treated_2 * (`x'==`l')	
	cap gen x`l'_iv_`m' = treated_2 * (`x'==`l')
	gen z`l' = tdcinitiative_2 * (`x'==`l')
}	
foreach l in ols iv {
	lab var x1_`l'_`m' "High $ \times \overline{support}_{fdt} $"
	lab var x0_`l'_`m' "Low $ \times \overline{support}_{fdt} $"
}

	eststo est_ols_`m' : reghdfe `y' x1_ols_`m' x0_ols_`m' `w', a(`fe') vce(cl F) nosamp	
	local N = e(N)
	local estlist_ols `estlist_ols' est_ols_`m'
	
	eststo est_iv_`m' : ivreghdfe `y' ( x1_iv_`m' x0_iv_`m' = z1 z0 ) `w', a(`fe') cl(F) ffirst
	drop z1 z0
	local estlist_iv `estlist_iv' est_iv_`m'
	
	matrix first = e(first)
	local F1_`m' = first[8,1]
	local F0_`m' = first[8,2]
	matrix drop first
	
	local ++m
	
}

	eststo stacked_`n' : appendmodels `estlist_ols' `estlist_iv'
	qui estadd scalar N `N'
local m=1	
foreach x of local dumlist {
	qui estadd scalar F1_`m' `F1_`m''
	qui estadd scalar F0_`m' `F0_`m''
	local ++m
}
	qui estadd local FE `fe'
	if "`w'"!=""	qui estadd local SC "Yes"
	if "`w'"==""	qui estadd local SC "No"
	qui estadd local LO "FPDY"		
	local mtitles `mtitles' "`:var lab `y''"
		
	eststo drop est*
	local ++n
	local ++nfe
	
}	
}
	

*** TABLE	
esttab using output\table_a7.tex, replace booktabs se label ///
	title("Effects of Export Support across Destination Income Groups") star(* 0.1 ** 0.05 *** 0.01) mtitles(`mtitles') keep(x?_*) ///
	stats(N F1_1 F0_1 LO FE SC , labels("Observations" "Parital IV F-stat, High" "Parital IV F-stat, Low" "Unit of observation" "Fixed Effects" "Size Control") fmt(%10.0fc %10.0fc %10.0fc "" "" "")) ///
	refcat(x1_ols_1 "OLS" x1_iv_1 "2SLS" , nolabel)	